import 'package:dio/dio.dart';

import '../repository/dto/banner_dto.dart';
import '../repository/dto/list_dto.dart';
import '../http/dio_instance.dart';
import 'dto/Knowledge_detail_dto.dart';
import 'dto/Knowledge_tree_dto.dart';
import 'dto/auth_dto.dart';

class Api {
  static Api instance = Api._();

  Api._();

  Future<List<BannerItemData?>?> getBanner() async {
    Response response = await DioInstance.instance().get(path: 'banner/json');
    BannerData bannerData = BannerData.fromJson(response.data);
    return bannerData.data;
  }

  Future<List<ListItemData?>?> getList(String pageCount) async {
    Response response = await DioInstance.instance().get(path: 'article/list/$pageCount/json');
    ListData listData = ListData.fromJson(response.data);
    return listData.datas;
  }

  Future<List<ListItemData?>?> getTopList() async {
    Response response = await DioInstance.instance().get(path: 'article/top/json');
    ListTopData listData = ListTopData.fromJson(response.data);
    return listData.data;
  }

  Future<bool?> register(String username, String password, String repassword) async {
    await DioInstance.instance().post(path: 'user/register', queryParameters: {
      'username': username,
      'password': password,
      'repassword': repassword,
    });
    return true;
  }
  
  Future<AuthData?> login(String username, String password) async {
    Response response = await DioInstance.instance().post(path: 'user/login', queryParameters: {
      'username': username,
      'password': password,
    });
    AuthData authData = AuthData.fromJson(response.data);
    return authData;
  }

  Future<bool?> logout() async {
    await DioInstance.instance().get(path: 'user/logout/json');
    return true;
  }

  
  Future<bool?> collect(int id) async {
    await DioInstance.instance().post(path: 'lg/collect/$id/json');
    return true;
  }

  Future<bool?> unCollect(int id) async {
    await DioInstance.instance().post(path: 'lg/uncollect_originId/$id/json');
    return true;
  }

  Future<List<KnowledgeTreeItem?>> getKnowledgeTree() async {
    Response response = await DioInstance.instance().get(path: 'tree/json');
    KnowledgeTreeData knowledgeTreeData = KnowledgeTreeData.fromJson(response.data);
    return knowledgeTreeData.data ?? [];
  }
  
  Future<List<KnowledgeDetailItem?>> getKnowledgeDetail(int id, int page) async {
    Response response = await DioInstance.instance().get(path: 'article/list/$page/json', queryParameters: {
      'cid': id,
    });
    KnowledgeDetailData knowledgeDetailData = KnowledgeDetailData.fromJson(response.data);
    return knowledgeDetailData.data?.datas ?? [];
  }
  
}